#!/bin/bash
#

#获取脚本当前目录路径
MY_PATH=$(cd `dirname $0`;pwd)
cd $MY_PATH

#定义颜色输出
_color(){
    key=$1
    shift
    case $key in
    -g)
        echo -e "\033[32m$@\033[0m"
        ;;
    -r)
        echo -e "\033[31m$@\033[0m"
        ;;
    -e)
        echo -e "\033[31m\033[5m$@\033[0m" && exit 1
        ;;
    -w)
        echo -e "\033[33m$@\033[0m"
        ;;
    *)
        echo "$@"
        ;;
    esac
}


##################安装依赖环境#########################
yum -y install libxml libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gcc python-devel mysql-devel sqlite-devel MySQL-python freetype-devel openssh-clients python-sqlite python-setuptools libxml2* libjpeg libjpeg-devel uwsgi-plugin-python2
[ $? -eq 0 ] && _color -g "依赖安装环境成功" || _color -e "依赖环境安装失败"

#######################升级openssl#####################
#openssl升级版本（版本大于1.0.1时无需升级）
cd $MY_PATH
tar -xf openssl-1.1.1d.tar.gz -C /usr/local/src/ && cd /usr/local/src/openssl-1.1.1d && ./config --prefix=/usr/local/openssl shared zlib  && make && make install
[ $? -eq 0 ] && _color -g "openssl安装成功" || _color -e "openssl安装失败"

#新openssl配置
cp /usr/bin/openssl /usr/bin/openssl.bak
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
echo "/usr/local/openssl/lib/" >> /etc/ld.so.conf.d/openssl_1.1.1.conf
ldconfig -v
openssl version -a

#######################安装安装python3.7#####################
#安装python3.7
cd $MY_PATH 
tar -zxvf Python-3.7.0.tgz -C /usr/local/src/ && cd /usr/local/src/Python-3.7.0 && ./configure --prefix=/usr/local/python37 --with-openssl=/usr/local/openssl/ && make && make install
[ $? -eq 0 ] && _color -g "python3.7安装成功" || _color -e "python3.7安装失败"

#设置系统默认python3
mv /usr/bin/python /usr/bin/python.bak
mv /usr/bin/pip /usr/bin/pip.bak
ln -sf /usr/local/python37/bin/python3 /usr/bin/python
ln -sf/usr/local/python37/bin/pip3 /usr/bin/pip
mv /usr/bin/python2 /usr/bin/python2.bak
ln -sf /usr/bin/python2.6 /usr/bin/python2

#修改yum继续使用python2.6
grep -q "/usr/bin/python2.6" /usr/bin/yum || sed -i 's@#!/usr/bin/python@#!/usr/bin/python2.6@' /usr/bin/yum

###################安装Python模块包########
url="-i http://pypi.douban.com/simple --trusted-host pypi.douban.com"
pip install $url pytz==2019.3
pip install $url uwsgi==2.0.17  && ln -sf /usr/local/python37/bin/uwsgi /usr/sbin/uwsgi
pip install $url celery==4.4.0  && ln -sf /usr/local/python37/bin/celery /usr/bin/celery
pip install $url supervisor==4.1.0  && ln -sf /usr/local/python37/bin/supervisord /usr/bin/supervisord && ln -sf /usr/local/python37/bin/supervisorctl /usr/bin/supervisorctl
pip install $url -r requirements.txt

###################安装django########
cd $MY_PATH 
tar xf Django-2.1.7.tar.gz -C /usr/local/src/ && cd /usr/local/src/Django-2.1.7 && python setup.py install
[ $? -eq 0 ] && _color -g "django2安装成功" || _color -e "django2安装失败"